package com.ye.personnel.common.response;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @author ye
 * @Description
 * @create 2021-09-01 16:19
 */
@ApiModel(value = "响应")
@Data
@NoArgsConstructor
public class BaseResult<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * 编码：1表示成功，其他值表示失败
     */
    @ApiModelProperty(value = "编码：1表示成功，其他值表示失败")
    private int status = 1;
    /**
     * 消息内容
     */
    @ApiModelProperty(value = "消息内容")
    private String message = "操作成功";
    /**
     * 响应数据
     */
    @ApiModelProperty(value = "响应数据")
    private T data;

    public boolean success() {
        return status == 1;
    }

    public BaseResult<T> ok(T data) {
        this.setData(data);
        return this;
    }

    public BaseResult<T> success(String message) {
        this.setMessage(message);
        return this;
    }

    public BaseResult<T> error(int code, String msg) {
        this.status = code;
        this.message = msg;
        return this;
    }

    public BaseResult<T> error(String msg) {
        this.status = ErrorCode.PARAMS_GET_ERROR;
        this.message = msg;
        return this;
    }

    public BaseResult(int status, String message) {
        this.status = status;
        this.message = message;
    }
}
